package com.xxpt.common;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xxpt.common.BaseRunnerListener;
import com.xxpt.common.constant.DelayedQueues;
import com.xxpt.common.utils.amqp.util.RabbitMqUtil;
import com.xxpt.model.Result;
import com.xxpt.model.user.User;
import com.xxpt.user.dto.AddUserDTO;
import com.xxpt.user.feign.AccountFeign;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Slf4j
@Component
public class ServerRunnerListener extends BaseRunnerListener {

    @Autowired
    AccountFeign accountFeign;

    @Autowired
    RabbitMqUtil rabbitMqUtil;

    @Override
    public void initialize() {
        //测试调用 user 服务
        //testFeign();
        //testMq();
    }

    /**
     * mq调用
     */
    private void testMq() {
        for (int i = 0; i < 500; i++) {
            JSONObject map = new JSONObject();
            map.put("body", "hello 江南一点雨"+new Date().toLocaleString());
            //普通消息
            //rabbitMqUtil.sendMessage(Queues.TES01_QUEUE, map);
            //延迟消息
            rabbitMqUtil.sendDelayedMessage(DelayedQueues.TES01_DELAYED_QUEUE, map);
            try {
                Thread.sleep(1000);
            } catch (Exception ex) {
                ex.printStackTrace();
                //System.out.println(ex.toString());
            }
        }
    }

    /**
     * 服务调用
     */
    private void testFeign() {
        AddUserDTO userDTO = new AddUserDTO();
        userDTO.setAccount("masl");
        userDTO.setName("山林");
        userDTO.setPassword("123456");
        userDTO.setSex(1);
        userDTO.setRoleId(1L);

        //Result<User> result = accountFeign.add(userDTO);
        //log.info(JSON.toJSONString(result));

        User user = accountFeign.findById(1L);
        log.info(JSON.toJSONString(user));
    }
}
